package sz.szsmk.citizencard.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import sz.szsmk.citizencard.R;

import java.util.List;

/**
 * Created by chensf on 17/10/3.
 */

public class ItemPopWindow extends PopupWindow {

    private static final String TAG = "FinishProjectPopupWindows";

    private LinearLayout mView;
    public Button btnSaveProject, btnAbandonProject, btnCancelProject;

    public ItemPopWindow(Activity context, final OnItemClickListener onItemClickListener, List<PopItem> popItems) {
        this(context, onItemClickListener, popItems, -1);
    }

    public ItemPopWindow(Activity context, final OnItemClickListener onItemClickListener, List<PopItem> popItems, int selectPositioned) {
        this(context, onItemClickListener, popItems, selectPositioned, context.getResources().getColor(R.color.qr_text_main));
    }

    public ItemPopWindow(Activity context, final OnItemClickListener onItemClickListener, List<PopItem> popItems, int selectPositioned, int defColor) {
        super(context);


        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = (LinearLayout) inflater.inflate(R.layout.qr_item_pop_window_root, null);

        LinearLayout.LayoutParams lineLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);

        Resources resources = context.getResources();
//        int textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 6, resources.getDisplayMetrics());

        for (int i = 0; i < popItems.size(); i++) {
            final PopItem popItem = popItems.get(i);
            TextView button = (TextView) inflater.inflate(R.layout.qr_item_pop_window_item, mView, false);
            button.setText(popItem.getName());
            if (selectPositioned == i) {
                button.setTextColor(Color.parseColor("#ff0000"));
            } else {
                button.setTextColor(defColor);
            }
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onItemClickListener) {
                        onItemClickListener.onItemClick(popItem);
                    }
                    dismiss();
                }
            });
            mView.addView(button);
            View lineView = new View(context);
            lineView.setLayoutParams(lineLp);
            mView.addView(lineView);
            if (i != popItems.size() - 1) {
                View divider = inflater.inflate(R.layout.qr_pop_divider, mView, false);
                mView.addView(divider);
                View lineView1 = new View(context);
                lineView1.setLayoutParams(lineLp);
                mView.addView(lineView1);
            }

        }

        TextView cancel = (TextView) inflater.inflate(R.layout.qr_item_pop_window_item_cancel, mView, false);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mView.addView(cancel);
        View lineView = new View(context);
        lineView.setLayoutParams(lineLp);
        mView.addView(lineView);

        this.setContentView(mView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.qr_BottomDialog);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffEEF0F3);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        backgroundAlpha(0.5f);//设置屏幕透明度(1.0f);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1.0f);//设置屏幕透明度(1.0f);
    }

    private void backgroundAlpha(float bgAlpha) {
        Context context = mView.getContext();
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = bgAlpha; //0.0-1.0
            activity.getWindow().setAttributes(lp);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(PopItem popItem);
    }

    public static class PopItem {
        private int position;
        private String name;

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
